package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.query.QueryEvaluateTargetBo;
import cn.cqie.frame.entity.EvaluateTarget;
import cn.cqie.frame.entity.EvaluateTargetOption;
import cn.cqie.frame.entity.EvaluateType;
import cn.cqie.frame.mapper.EvaluateTargetMapper;
import cn.cqie.frame.service.EvaluateTargetOptionService;
import cn.cqie.frame.service.EvaluateTargetService;
import cn.cqie.frame.service.EvaluateTypeService;
import cn.cqie.frame.vo.EvaluateTargetTreeData;
import cn.cqie.frame.vo.EvaluateTargetVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评价指标 服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-12
 */
@Service
public class EvaluateTargetServiceImpl extends ServiceImpl<EvaluateTargetMapper, EvaluateTarget> implements EvaluateTargetService {
    @Autowired
    private EvaluateTargetService evaluateTargetService;
    @Autowired
    private EvaluateTargetOptionService evaluateTargetOptionService;
    @Autowired
    private EvaluateTypeService evaluateTypeService;

    /**
     * 插入评价指标
     * @param po
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public EvaluateTargetVo saveEvaluateTarget(QueryEvaluateTargetBo po) {
        EvaluateTarget mo = new EvaluateTarget();
        BeanUtils.copyProperties(po, mo);
        evaluateTargetService.save(mo);
        if (po.getEvaluateTargetOptionList() != null && po.getEvaluateTargetOptionList().size() > 0) {
            for (EvaluateTargetOption item : po.getEvaluateTargetOptionList()) {
                //设置主表Id
                item.setEvaluateTargetId(mo.getId());
            }
            evaluateTargetOptionService.saveBatch(po.getEvaluateTargetOptionList());
        }
        EvaluateTargetVo vo = mappingMoToPo(mo, true);
        return vo;
    }

    /**、
     * 删除评价指标
     * @param evaluateTargetId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public Boolean deleteEvaluateTarget(Integer evaluateTargetId) {
        //删除子表
        QueryWrapper<EvaluateTargetOption> qwDetail = new QueryWrapper<>();
        qwDetail.eq("evaluateTargetId", evaluateTargetId);
        boolean remove = evaluateTargetOptionService.remove(qwDetail);
        //删除主表
        boolean b = evaluateTargetService.removeById(evaluateTargetId);
        return remove || b;
    }

    /**
     * 更新评价指标
     * @param po
     * @return
     */
    @Override
    public EvaluateTargetVo updateEvaluateTarget(QueryEvaluateTargetBo po) {
        EvaluateTarget mo = new EvaluateTarget();
        BeanUtils.copyProperties(po, mo);
        evaluateTargetService.updateById(mo);
        QueryWrapper<EvaluateTargetOption> qwDetail = new QueryWrapper<>();
        qwDetail.eq("evaluateTargetId", mo.getId());
        List<EvaluateTargetOption> crudeDetails = evaluateTargetOptionService.list(qwDetail);
        //默认全部删除
        List<Long> delIdRange = crudeDetails.stream().map(EvaluateTargetOption::getId).collect(Collectors.toList());
        for (EvaluateTargetOption detail : po.getEvaluateTargetOptionList()) {
            if (detail.getId() != null) {
                //提交数据有Id，以修改方式更新，不删除，因此从删除范围移出
                delIdRange.remove(detail.getId());
            }
        }
        //2.2新增或修改方式更新从表
        evaluateTargetOptionService.saveOrUpdateBatch(po.getEvaluateTargetOptionList());
        //2.3删除从表
        if (delIdRange.size() > 0) {
            QueryWrapper<EvaluateTargetOption> qwDelete = new QueryWrapper<>();
            qwDelete.in("id", delIdRange);
            evaluateTargetOptionService.remove(qwDelete);
        }
        //3.构造返回值(mo转vo)
        EvaluateTargetVo vo = mappingMoToPo(mo, true);
        return vo;
    }

    /**
     * 评价指标树
     * @return
     */
    @Override
    public List<EvaluateTargetTreeData> listTreeEvaluateTarget() {
        List<EvaluateTargetTreeData> ret = new ArrayList<>();
        //全部指标
        List<EvaluateTarget> allEvaluateTarget = evaluateTargetService.list();
        //全部备选项
        List<EvaluateTargetOption> allEvaluateTargetOption = evaluateTargetOptionService.list();
        //全部评价类型
        List<EvaluateType> allEvaluateType = evaluateTypeService.list();

        List<EvaluateTarget> first = allEvaluateTarget.stream().
                filter(x -> x.getParentId() == null).
                sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        for (EvaluateTarget mo : first) {
            ret.add(generateTreeData(mo, allEvaluateTarget, allEvaluateTargetOption, allEvaluateType));
        }
        return ret;
    }
    /**
     * 递归方法--EvaluateTarget-->EvaluateTargetTreeData
     * 要映射评价类型+从表备选项列表
     * 构造树表数据用
     */
    private EvaluateTargetTreeData generateTreeData(EvaluateTarget mo, List<EvaluateTarget> allEvaluateTarget, List<EvaluateTargetOption> allEvaluateTargetOption, List<EvaluateType> allEvaluateType) {
        EvaluateTargetTreeData node = new EvaluateTargetTreeData();
        BeanUtils.copyProperties(mo, node);
        // 映射--上级指标名称
        if (mo.getParentId() != null) {
            EvaluateTarget parentEvaluateTarget = allEvaluateTarget.stream().filter(x -> x.getId().equals(mo.getParentId())).findFirst().orElse(null);
            if (parentEvaluateTarget != null) {
                node.setParentName(parentEvaluateTarget.getName());
            }
        }
        //映射--评价类型名称
        if (mo.getEvaluateTypeId() != null) {
            EvaluateType evaluateType = allEvaluateType.stream().filter(x -> x.getId().equals(mo.getEvaluateTypeId())).findFirst().orElse(null);
            if (evaluateType != null) {
                node.setEvaluateTypeName(evaluateType.getName());
            }
        }
        //映射--备选项列表
        List<EvaluateTargetOption> details = allEvaluateTargetOption.stream().filter(x -> x.getEvaluateTargetId().equals(mo.getId())).collect(Collectors.toList());
        node.setEvaluateTargetOptionList(details);
        //映射--下级指标
        List<EvaluateTarget> children = allEvaluateTarget.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getId()))
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        List<EvaluateTargetTreeData> subNodeList = new ArrayList<>();
        for (EvaluateTarget child : children) {
            subNodeList.add(generateTreeData(child, allEvaluateTarget, allEvaluateTargetOption, allEvaluateType));
        }
        node.setChildren(subNodeList);
        return node;
    }
    /**
        * 递归方法--EvaluateTarget-->EvaluateTargetTreeData
     * 不映射评价类型+从表备选项列表
     * 构造上级指标备选项用
     */
    private EvaluateTargetTreeData generateTreeData(EvaluateTarget mo, List<EvaluateTarget> allEvaluateTarget) {
        EvaluateTargetTreeData node = new EvaluateTargetTreeData();
        BeanUtils.copyProperties(mo, node);
        // 映射--上级指标名称
        if (mo.getParentId() != null) {
            EvaluateTarget parentEvaluateTarget = allEvaluateTarget.stream().filter(x -> x.getId().equals(mo.getParentId())).findFirst().orElse(null);
            if (parentEvaluateTarget != null) {
                node.setParentName(parentEvaluateTarget.getName());
            }
        }
        //映射--下级指标
        List<EvaluateTarget> children = allEvaluateTarget.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getId()))
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        List<EvaluateTargetTreeData> subNodeList = new ArrayList<>();
        for (EvaluateTarget child : children) {
            subNodeList.add(generateTreeData(child, allEvaluateTarget));
        }
        node.setChildren(subNodeList);
        return node;
    }
    /**
     * 上级指标备选项
     * @return
     */
    @Override
    public List<EvaluateTargetTreeData> listParentData(Long id) {
        List<EvaluateTargetTreeData> ret = new ArrayList<>();
        //限制不跨类型选上级指标(更完善的方案是评价类型确定后实时更新上级备选项)
        QueryWrapper<EvaluateTarget> qw = new QueryWrapper<>();
        if (id != null) {
            qw.inSql("evaluateTypeId", "select evaluateTypeId from evaluate_target where id=" + id);
        }
        List<EvaluateTarget> all = evaluateTargetService.list(qw);
        List<Long> excludeIdList = new ArrayList<>();
        if (id != null) {
            //排除自己及全部后代
            addSelfAndChildren(id, excludeIdList, all);
            all = all.stream().filter(x -> excludeIdList.contains(x.getId()) == false).collect(Collectors.toList());
        }
        List<EvaluateTarget> first = all.stream()
                .filter(x -> x.getParentId() == null)
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        for (EvaluateTarget mo : first) {
            ret.add(generateTreeData(mo, all));
        }
        
        return ret;
    }



    /**
     * 递归方法--添加自己及子女到idList
     * 构造上级指标备选项用
     */
    private void addSelfAndChildren(Long selfId, List<Long> idList, List<EvaluateTarget> all){
        idList.add(selfId);
        //查找子女
        List<EvaluateTarget> children = all.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(selfId))
                .collect(Collectors.toList());
        //递归添加子女的Id
        for (EvaluateTarget child : children) {
            addSelfAndChildren(child.getId(), idList, all);
        }
    }

    /**
     * 映射mo为vo
     */
    private EvaluateTargetVo mappingMoToPo(EvaluateTarget mo, Boolean isContainDetails) {
        EvaluateTargetVo vo = new EvaluateTargetVo();
            BeanUtils.copyProperties(mo, vo);
            //映射--上级指标名称
            if (mo.getParentId() != null) {
                EvaluateTarget parentEvaluateTarget = evaluateTargetService.getById(mo.getParentId());
                if (parentEvaluateTarget != null) {
                    vo.setParentName(parentEvaluateTarget.getName());
                }
            }
            //映射--评价类型名称
            if (mo.getEvaluateTypeId() != null) {
                EvaluateType evaluateType = evaluateTypeService.getById(mo.getEvaluateTypeId());
                if (evaluateType != null) {
                    vo.setEvaluateTypeName(evaluateType.getName());
                }
            }
            //映射--备选项列表
             //需要子表的明细
            if (isContainDetails) {
                QueryWrapper<EvaluateTargetOption> qw = new QueryWrapper<>();
                qw.eq("evaluateTargetId", mo.getId());
                vo.setEvaluateTargetOptionList(evaluateTargetOptionService.list(qw));
            }
            return vo;
        }

    /**
     * 批量导入指标
      * @param file
     * @return
     */
    @Override
    public Boolean importExcel(MultipartFile file) throws Exception {
        InputStream is = file.getInputStream();
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        Long typeId;
        int rowIndex;
        //1.学生评教
        typeId = 1L;
        HSSFSheet sheet = wb.getSheet("学生评教");
        List<EvaluateTarget> targetList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            EvaluateTarget target = new EvaluateTarget();
            target.setEvaluateTypeId(typeId);
            rowIndex = 4 * i + 1;
            HSSFRow row = sheet.getRow(rowIndex);
            target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            target.setSortCode((i + 1) * 10);
            targetList.add(target);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        List<EvaluateTargetOption> optionList = new ArrayList<>();
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //2.教师自评
        typeId = 2L;
        sheet = wb.getSheet("教师自评");
        targetList = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            EvaluateTarget target = new EvaluateTarget();
            target.setEvaluateTypeId(typeId);
            rowIndex = 4 * i + 1;
            HSSFRow row = sheet.getRow(rowIndex);
            target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            target.setSortCode((i + 1) * 10);
            targetList.add(target);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //3.同行评教
        typeId = 3L;
        sheet = wb.getSheet("同行评教");
        targetList = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            EvaluateTarget target = new EvaluateTarget();
            target.setEvaluateTypeId(typeId);
            rowIndex = 4 * i + 1;
            HSSFRow row = sheet.getRow(rowIndex);
            target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            target.setSortCode((i + 1) * 10);
            targetList.add(target);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //4.教研室主任评教
        typeId = 4L;
        sheet = wb.getSheet("教研室主任评教");
        rowIndex = 1;
        HSSFRow row = sheet.getRow(rowIndex);
        EvaluateTarget firstTarget = new EvaluateTarget();
        firstTarget.setEvaluateTypeId(typeId);
        firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
        firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
        firstTarget.setSortCode(10);
        evaluateTargetService.save(firstTarget);
        targetList = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            EvaluateTarget secondTarget = new EvaluateTarget();
            secondTarget.setEvaluateTypeId(typeId);
            secondTarget.setParentId(firstTarget.getId());
            rowIndex = 4 * i + 1;
            row = sheet.getRow(rowIndex);
            secondTarget.setName(row.getCell(2).getRichStringCellValue().getString().trim());
            secondTarget.setWeight(new BigDecimal(row.getCell(3).getNumericCellValue()));
            secondTarget.setSortCode((i + 1) * 10);
            targetList.add(secondTarget);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 1;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(4).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(5).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        targetList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            EvaluateTarget target = new EvaluateTarget();
            target.setEvaluateTypeId(typeId);
            rowIndex = 4 * i + 37;
            row = sheet.getRow(rowIndex);
            target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            target.setSortCode((i + 2) * 10);
            targetList.add(target);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 37;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(4).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(5).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //5.系主任评教
        typeId = 5L;
        sheet = wb.getSheet("系主任评教");
        //量考核
        rowIndex = 1;
        row = sheet.getRow(rowIndex);
        firstTarget = new EvaluateTarget();
        firstTarget.setEvaluateTypeId(typeId);
        firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
        firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
        firstTarget.setSortCode(10);
        evaluateTargetService.save(firstTarget);
        targetList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            EvaluateTarget secondTarget = new EvaluateTarget();
            secondTarget.setEvaluateTypeId(typeId);
            secondTarget.setParentId(firstTarget.getId());
            rowIndex = 4 * i + 1;
            row = sheet.getRow(rowIndex);
            secondTarget.setName(row.getCell(2).getRichStringCellValue().getString().trim());
            secondTarget.setWeight(new BigDecimal(row.getCell(3).getNumericCellValue()));
            secondTarget.setSortCode((i + 1) * 10);
            targetList.add(secondTarget);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 4 * index + j + 1;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //质考核
        rowIndex = 13;
        row = sheet.getRow(rowIndex);
        firstTarget = new EvaluateTarget();
        firstTarget.setEvaluateTypeId(typeId);
        firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
        firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
        firstTarget.setSortCode(20);
        evaluateTargetService.save(firstTarget);
        //工作态度
        EvaluateTarget secondTarget = new EvaluateTarget();
        secondTarget.setEvaluateTypeId(typeId);
        secondTarget.setParentId(firstTarget.getId());
        secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
        secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
        secondTarget.setSortCode(10);
        evaluateTargetService.save(secondTarget);
        //接受任务态度
        EvaluateTarget threeTarget = new EvaluateTarget();
        threeTarget.setEvaluateTypeId(typeId);
        threeTarget.setParentId(secondTarget.getId());
        threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
        threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
        threeTarget.setSortCode(10);
        evaluateTargetService.save(threeTarget);
        //备选项
        for (int j = 0; j < 4; j++) {
            rowIndex = 13 + j;
            EvaluateTargetOption option = new EvaluateTargetOption();
            option.setEvaluateTargetId(threeTarget.getId());
            option.setContent(sheet.getRow(rowIndex).getCell(8).getRichStringCellValue().getString().trim());
            option.setScore(new BigDecimal(sheet.getRow(rowIndex).getCell(9).getNumericCellValue()));
            option.setSortCode((j + 1) * 10);
            optionList.add(option);
        }
        //教学常规
        rowIndex = 17;
        threeTarget = new EvaluateTarget();
        threeTarget.setEvaluateTypeId(typeId);
        threeTarget.setParentId(secondTarget.getId());
        threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
        threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
        threeTarget.setSortCode(20);
        evaluateTargetService.save(threeTarget);
        targetList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            rowIndex = 4 * i + 17;
            EvaluateTarget fourTarget = new EvaluateTarget();
            fourTarget.setEvaluateTypeId(typeId);
            fourTarget.setParentId(threeTarget.getId());
            fourTarget.setName(sheet.getRow(rowIndex).getCell(6).getRichStringCellValue().getString().trim());
            fourTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(7).getNumericCellValue()));
            fourTarget.setSortCode(10 * (i + 1));
            targetList.add(fourTarget);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 18 + 4 * index + j;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        //学术与研究水平
        rowIndex = 45;
        secondTarget = new EvaluateTarget();
        secondTarget.setEvaluateTypeId(typeId);
        secondTarget.setParentId(firstTarget.getId());
        secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
        secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
        secondTarget.setSortCode(20);
        evaluateTargetService.save(secondTarget);
        targetList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            rowIndex = 45 + 4 * i;
            threeTarget = new EvaluateTarget();
            threeTarget.setEvaluateTypeId(typeId);
            threeTarget.setParentId(secondTarget.getId());
            threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
            threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
            threeTarget.setSortCode((i + 1) * 10);
            targetList.add(threeTarget);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 45 + 4 * index + j;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        targetList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            rowIndex = 57 + 4 * i;
            secondTarget = new EvaluateTarget();
            secondTarget.setEvaluateTypeId(typeId);
            secondTarget.setParentId(firstTarget.getId());
            secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
            secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
            secondTarget.setSortCode((i + 1) * 10);
            targetList.add(secondTarget);
        }
        evaluateTargetService.saveBatch(targetList);
        //备选项
        for (EvaluateTarget target : targetList) {
            int index = targetList.indexOf(target);
            for (int j = 0; j < 4; j++) {
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(target.getId());
                rowIndex = 57 + 4 * index + j;
                row = sheet.getRow(rowIndex);
                option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
        }
        boolean b = evaluateTargetOptionService.saveBatch(optionList);
        return b;
    }

}
